import http from '@/utils/http';
import type { User, ApiResponse } from '@/types';

// 登录接口
export interface LoginRequest {
  email: string;
  password: string;
}

export interface LoginResponse {
  token: string;
  user: User;
}

// 注册接口
export interface RegisterRequest {
  email: string;
  password: string;
  username?: string;
  nickname?: string;
}

// 更新用户资料接口
export interface UpdateProfileRequest {
  username?: string;
  nickname?: string;
  phone?: string;
  avatar?: string;
}

// 修改密码接口
export interface ChangePasswordRequest {
  currentPassword: string;
  newPassword: string;
}

// 登录信息记录
export interface LoginInfo {
  ip: string;
  province: string;
  city: string;
  created_at: string;
}

/**
 * 用户登录
 */
export const login = async (data: LoginRequest): Promise<LoginResponse> => {
  const response = await http.post('/auth/login', data);
  return response.data.data; // 注意这里需要访问 data.data
};

/**
 * 用户注册
 */
export const register = async (data: RegisterRequest): Promise<LoginResponse> => {
  const response = await http.post('/auth/register', data);
  return response.data.data; // 注意这里需要访问 data.data
};

/**
 * 获取当前用户信息
 */
export const getCurrentUser = async (): Promise<User> => {
  const response = await http.get('/auth/me');
  return response.data.data; // 注意这里需要访问 data.data
};

/**
 * 更新用户资料
 */
export const updateProfile = async (data: UpdateProfileRequest): Promise<User> => {
  const response = await http.put('/auth/profile', data);
  return response.data.data; // 注意这里需要访问 data.data
};

/**
 * 修改密码
 */
export const changePassword = async (data: ChangePasswordRequest): Promise<void> => {
  await http.put('/auth/password', data);
  // 密码修改接口只返回成功消息，不需要返回数据
};

/**
 * 获取登录信息
 */
export const getLoginInfo = async (): Promise<LoginInfo[]> => {
  const response = await http.get('/auth/loginInfo');
  return response.data.data; // 注意这里需要访问 data.data
};

/**
 * 退出登录
 */
export const logout = async (): Promise<void> => {
  await http.post('/auth/logout');
  // 登出接口只返回成功消息，不需要返回数据
}; 